package com.xge.business.utils

object ZodiacUtils {

    private val chineseZodiac = arrayOf(
        "鼠", "牛", "虎", "兔", "龙", "蛇",
        "马", "羊", "猴", "鸡", "狗", "猪"
    )

    /**
     * 根据输入的年份字符串，返回对应的生肖名称
     * @param yearStr 年份字符串，如 "1990"、"2024"
     * @return 对应生肖或错误提示
     */
    fun getZodiac(yearStr: String): String {
        val year = yearStr.toIntOrNull()
        if (year == null || year < 0) {
            return "111" //输入年份错误
        }
        return getZodiacByYear(year)
    }

    /**
     * 根据年份整数返回生肖
     * @param year 公历年
     * @return 对应生肖名称
     */
    private fun getZodiacByYear(year: Int): String {
        val index = (year - 1900) % 12
        return chineseZodiac[(index + 12) % 12]  // 防止负数
    }
}
